package com.bruce.controller;

import com.bruce.utils.VerifyCode;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * 生成随机验证码
 */
@WebServlet("/codeServlet.do")
public class CodeServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletOutputStream outputStream = resp.getOutputStream();
        VerifyCode verifyCode=new VerifyCode();
        BufferedImage bufferedImage = verifyCode.getImage();
        //系统生成的验证码
        String codeText = verifyCode.getText();
        System.out.println("系统生成的验证码是:"+codeText);
        //把系统生成的验证码存储起来
        HttpSession session = req.getSession();
        session.setAttribute("syscode",codeText);
        //输出一张随机的验证码图片
        verifyCode.output(bufferedImage,outputStream);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }
}
